<?php
/*
 * odbc
 *
 * 创建数据库有误
 * 字符乱码
 *
 */


header("Content-type: text/html; charset=utf-8");

// mySql
$mysql_server_name = "MySql";       // 数据源
$mysql_username = "root";           // 连接数据库用户名
$mysql_password = "";                // 连接数据库密码
$mysql_database = "my_db";          // 数据库名称

$conn = odbc_connect($mysql_server_name, $mysql_username, $mysql_password);
if (!$conn) {
    exit("Connection Failed: " . $conn);
}
/*
// 有错误
if (odbc_exec($con, "CREATE DATABASE IF NOT EXISTS $mysql_database" )) {
    echo "Database created" .'<br>';
} else {
    echo "Error creating database: " . mysql_error() . '<br>';
}
*/

$sql = "CREATE TABLE IF NOT EXISTS Persons
(
personID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(personID),
FirstName varchar(15),
LastName varchar(15),
Age int
)";

$rs = odbc_exec($conn, $sql);
if (!$rs) {
    exit("Error in CREATE TABLE");
}

// INSERT
if (odbc_exec($conn,"INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Glenn', 'Quagmire', '33')")) {
    echo "INSERT Glenn", '<br>';
}

if (odbc_exec($conn, "INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('高峰', 'Quag地方vds123', '21')")) {
    echo "INSERT 高峰", '<br>';
}

// SELECT
$sql = "SELECT * FROM Persons";
$rs = odbc_exec($conn, $sql);
if (!$rs) {
    exit("Error in SELECT");
}

echo 'SELECT:<br>';
while (odbc_fetch_row($rs)) {
    $personID = odbc_result($rs, "personID");
    $FirstName = odbc_result($rs, "FirstName");
    $LastName = odbc_result($rs, "LastName");
    $Age = odbc_result($rs, "Age");

    echo $personID,' ', $FirstName, ' ',$LastName, ' ', $Age , '<br>';
}

odbc_exec($conn, "DELETE FROM Persons");

odbc_close($conn);
